package com.easy.work.function.resume;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.base.syslib.util.DateTimeHelper;
import com.base.syslib.util.MessageTools;
import com.base.syslib.util.TextUtil;
import com.easy.work.R;
import com.easy.work.app.AppSession;
import com.easy.work.app.BaseActivity;
import com.easy.work.function.me.adapter.WorkExperienceAdapter;
import com.easy.work.http.BaseResponse;
import com.easy.work.http.HttpClient;
import com.easy.work.http.request.WorkExperienceRequest;
import com.easy.work.http.response.WorkExperienceResponse;
import com.easy.work.utils.StringUtil;
import com.easy.work.view.dialog.DateDialog;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by admin on 2017/12/27.
 * 添加工作经历
 */
public class AddWorkExperienceAct extends BaseActivity {
    @BindView(R.id.tv_ruzhishijian)
    TextView tvRuzhishijian;
    @BindView(R.id.ly_ruzhishijian)
    LinearLayout lyRuzhishijian;
    @BindView(R.id.tv_lizhishijian)
    TextView tvLizhishijian;
    @BindView(R.id.ly_lizhishijian)
    LinearLayout lyLizhishijian;
    @BindView(R.id.et_company_name)
    EditText etCompanyName;
    @BindView(R.id.et_work_introduction)
    EditText etWorkIntroduction;
    @BindView(R.id.btn_add)
    Button btnAdd;
    @BindView(R.id.et_zhiwei)
    EditText etZhiwei;

    /**
     * 工作经历，编辑进来之后有数据
     */
    private WorkExperienceResponse workExperienceResponse;

    public static void startActivity(Context context, WorkExperienceResponse workExperienceResponse) {
        Intent intent = new Intent(context, AddWorkExperienceAct.class);
        intent.putExtra("workExperienceResponse", workExperienceResponse);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_add_resume);
        ButterKnife.bind(this);
        initTitle("添加工作经历");
        if (getIntent().hasExtra("workExperienceResponse")) {
            workExperienceResponse = (WorkExperienceResponse) getIntent().getSerializableExtra("workExperienceResponse");
            if (workExperienceResponse == null) {
                return;
            }
            tvRuzhishijian.setText(workExperienceResponse.dateFrom);
            tvLizhishijian.setText(workExperienceResponse.dateTo);
            etCompanyName.setText(workExperienceResponse.company);
            etZhiwei.setText(workExperienceResponse.job);
            etWorkIntroduction.setText(workExperienceResponse.description);
        }
    }

    @OnClick({R.id.ly_ruzhishijian, R.id.ly_lizhishijian, R.id.btn_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ly_ruzhishijian:
                selectDate(tvRuzhishijian);
                break;
            case R.id.ly_lizhishijian:
                selectDate(tvLizhishijian);
                break;
            case R.id.btn_add:
                if (isOverExperience()) {
                    commitWorkExperience();
                }

                break;
        }
    }

    /**
     * 选择日期
     */
    private void selectDate(final TextView textView) {
        new DateDialog(this, textView.getText().toString().trim(), new DateDialog.DateDialogValue() {

            @Override
            public boolean findDateDialogValue(final String date) {
                String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
                Long diffDate = DateTimeHelper.getDaysOfTowDiffDate(date, today);
                if (diffDate < 0) {
                    MessageTools.showToast(getApplicationContext(), "请设置今天之前的日期");
                    return false;
                }
                textView.setText(date);
                return true;
            }
        }).builder().setPositiveButton("确定", null).setNegativeButton("取消", null).show();
    }


    WorkExperienceRequest request = new WorkExperienceRequest();

    /**
     * 提交工作经历
     */
    private void commitWorkExperience() {
        request.userId = AppSession.getInstance().getUserId();
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                WorkExperienceResponse response1 = (WorkExperienceResponse) response;
                EventBus.getDefault().post(response1);
                finish();
            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                MessageTools.showToast(oThis, msg);
            }
        });

    }

    /**
     * 经历是否填写完整
     *
     * @return
     */
    private boolean isOverExperience() {
        if (workExperienceResponse == null) {
            workExperienceResponse = new WorkExperienceResponse();
        }
        String dateFrom = tvRuzhishijian.getText().toString();
        if (StringUtil.isEmpty(dateFrom)) {
            MessageTools.showToast(oThis, "请选择您的入职时间");
            return false;
        }
        String dateTo = tvLizhishijian.getText().toString();
        if (StringUtil.isEmpty(dateTo)) {
            MessageTools.showToast(oThis, "请选择您的离职时间");
            return false;
        }

        String company = etCompanyName.getText().toString();
        if (StringUtil.isEmpty(company)) {
            MessageTools.showToast(oThis, "请输入您的公司名");
            return false;
        }
        String job = etZhiwei.getText().toString();
        if (StringUtil.isEmpty(job)) {
            MessageTools.showToast(oThis, "请输入您的职位");
            return false;
        }
        String description = etWorkIntroduction.getText().toString();
        if (StringUtil.isEmpty(description)) {
            MessageTools.showToast(oThis, "请输入您的工作简介");
            return false;
        }
        if (TextUtil.isNotNull(workExperienceResponse.experienceId)) {
            request.experienceId = workExperienceResponse.experienceId;
        }
        request.dateFrom = dateFrom;
        request.dateTo = dateTo;
        request.company = company;
        request.job = job;
        request.description = description;
        return true;
    }
}
